package com.isdepci.project.factor.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.isdepci.common.utils.DateUtils;
import com.isdepci.common.utils.StringUtils;
import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.factor.domain.ProductStage;
import com.isdepci.project.factor.domain.StandardStage;
import com.isdepci.project.factor.mapper.ProductConsumablesMapper;
import com.isdepci.project.factor.mapper.ProductStageMapper;
import com.isdepci.project.factor.mapper.StandardStageMapper;
import com.isdepci.project.factor.utils.FactorRedisUtils;
import com.isdepci.project.factor.utils.FactorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.isdepci.project.factor.mapper.ProductInfoMapper;
import com.isdepci.project.factor.domain.ProductInfo;
import com.isdepci.project.factor.service.IProductInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 产品Service业务层处理
 *
 * @author isdepci
 * @date 2024-10-12
 */
@Service
public class ProductInfoServiceImpl implements IProductInfoService {

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Autowired
    private ProductStageMapper productStageMapper;

    @Autowired
    private ProductConsumablesMapper productConsumablesMapper;
    @Autowired
    private FactorUtils factorUtils;

    /**
     * 查询产品
     */
    @Override
    public ProductInfo selectProductInfoById(Long id) {
        ProductInfo info = productInfoMapper.selectProductInfoById(id);
        if(info!=null){
            info.setStageList(productStageMapper.selectStageListByProductId(id));
        }
        return info;
    }

    /**
     * 查询产品列表
     */
    @Override
    public List<ProductInfo> selectProductInfoList(ProductInfo productInfo) {
        return productInfoMapper.selectProductInfoList(productInfo);
    }

    /**
     * 新增产品
     */
    @Override
    public AjaxResult insertProductInfo(ProductInfo productInfo) {
        if(StringUtils.isEmpty(productInfo.getName())){
            return AjaxResult.error("名称不能为空！");
        }
        List<ProductInfo> list = productInfoMapper.selectListByName(productInfo.getName());
        if (list.size()>0){
            return AjaxResult.error("新增'" + productInfo.getName() + "'失败，名称已存在");
        }
        productInfo.setCreateUser();
        int row = productInfoMapper.insertProductInfo(productInfo);
        if (row > 0) {
            genProductStageByInsert(productInfo.getId());
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 修改产品
     */
    @Override
    public AjaxResult updateProductInfo(ProductInfo productInfo) {
        ProductInfo old = productInfoMapper.selectProductInfoById(productInfo.getId());
        if(StringUtils.isNotEmpty(productInfo.getName())&& !old.getName().equals(productInfo.getName())){
            List<ProductInfo> list = productInfoMapper.selectListByName(productInfo.getName());
            if (list.size()>0){
                return AjaxResult.error("修改'" + productInfo.getName() + "'失败，名称已存在");
            }
        }
        productInfo.setUpdateUser();
        int row = productInfoMapper.updateProductInfo(productInfo);
        if (row > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 批量删除产品
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteProductInfoByIds(Long[] ids) {
        productStageMapper.deleteListByProductIds(ids);
        productConsumablesMapper.deleteListByProductIds(ids);
        return  productInfoMapper.deleteProductInfoByIds(ids);
    }

    /**
     * 生成新产品的阶段节点
     */
    public void genProductStageByInsert(Long productId){
        List<StandardStage> standardStages = factorUtils.selectStandardStageDictList();
        if(standardStages==null||standardStages.size()<=0){
            return;
        }
        List<Long> oldIds = new ArrayList<>();
        List<ProductStage> productStages = new ArrayList<>();
        for (StandardStage standardStage : standardStages) {
            productStages.add(new ProductStage(productId,standardStage));
            oldIds.add(standardStage.getId());
        }
        productStageMapper.insertList(productStages);
        List<Long> newIds = productStages.stream().map(ProductStage::getId).collect(Collectors.toList());
        for (ProductStage productStage : productStages) {
            if(productStage.getParentId()==0){
                continue;
            }
            productStage.setParentId(newIds.get(oldIds.indexOf(productStage.getParentId())));
        }
        productStageMapper.updateList(productStages);
    }



}
